<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Document</title>
    <!-- 引入react核心库 -->
    <script type="text/javascript" src="../../js/react.development.js"></script>
    <!-- 用于支持react操作DOM -->
    <script type="text/javascript" src="../../js/react-dom.development.js"></script>
    <!-- 用于将jsx转为js -->
    <script type="text/javascript" src="../../js/babel.min.js"></script>
    <!-- 用于对组件标签属性进行限制 -->
    <script type="text/javascript" src="../../js/prop-types.js"></script>
</head>
<body>
    <div id="test"></div>

    <script type="text/babel">
        class MyComponent extends React.Component{
            state = {
                opacity: 1
            }
            // 组件挂载后操作
            componentDidMount(){
                this.timer = setInterval(()=>{
                    console.log("runing...");
                    let {opacity} = this.state
                    if (opacity<=0){
                        opacity = 1
                    }
                    this.setState({opacity: opacity-0.1})
                },200)
            }
            // 组件即将卸载
            componentWillUnmount(){
                clearInterval(this.timer)
            }
            death = (event)=>{
                event.preventDefault()
                // 卸载组件
                ReactDOM.unmountComponentAtNode(document.getElementById("test"))
            }
            render(){
                return (
                    <form onSubmit={this.death}>
                        <h2 style={{opacity:this.state.opacity}}>React学不会，怎么办</h2>
                        <button>不活了</button>
                    </form>
                )
            }
        }
        
        let p ={}
        ReactDOM.render(<MyComponent {...p}/>,document.getElementById("test"))
    </script>
</body>
<style>
</style>
</html>